package com.pashanhoo.cpu;


import java.util.Random;

/** CPU的分支预测
 * @author 胡庆安
 * @version V1.0
 * @Package com.pashanhoo.cpu
 * @date 2020/11/4 9:30
 * @Copyright © 2010-2020 爬山虎科技（扬州）股份有限公司
 */
public class Test {



    public static void main(String[] args) {
        Test01();
    }
    static void Test01(){
        int arraySize = 32768;
        int data[] = new int[arraySize];

        Random rnd = new Random(0);
        for (int c= 0;c<arraySize;++c){
            data[c] = rnd.nextInt()%256;
        }
        long sum = 0;


        long l1 = System.currentTimeMillis();
        for (int i=0;i<10000;++i){
            for(int c = 0; c<arraySize; ++c){
                if (data[c]>=128){
                    sum += data[c];
                }
            }
        }
        long l2 = System.currentTimeMillis();

        System.out.println(l2-l1);

        long l3 = System.currentTimeMillis();
        for(int c = 0; c<arraySize; ++c){
            for (int i=0;i<10000;i++){
                if (data[c]>=128){
                    sum += data[c];
                }
            }

        }
        long l4 = System.currentTimeMillis();
        System.out.println(l4-l3);

    }
    static void Test02(){

    }
}
